import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:convert';
import 'methodchannel_mgr.dart';

class page_test_method_channel extends StatefulWidget {
  static const String routeName = "/page_test_method_channel";
  @override
  State<StatefulWidget> createState() {
    // 创建 State 对象并返回
    return _HomeContentState();
  }
}

// 定义 State 子类
class _HomeContentState extends State<StatefulWidget> {
  @override
  Widget build(BuildContext context) {
    // 构建自己的 Widget
    // return const Text("dart");
    return Scaffold(
        appBar: AppBar(
          leading: GestureDetector(
            child: Icon(Icons.arrow_back),
            onTap: () => onBackPressed(context),
          ),
          title: Text('Page C'),
        ),
        // body: Container(),
        body: ElevatedButton(
          onPressed: () {
            invokeNativeMethod();
          },
          child: Text('invokeNativeMethod'),
        ));
  }

  void onBackPressed(BuildContext context) {
    NavigatorState navigatorState = Navigator.of(context);
    if (navigatorState.canPop()) {
      navigatorState.pop();
    } else {
      SystemNavigator.pop();
    }
  }

  Future<void> invokeNativeMethod() async {
    var result = await MyMethodChannel.callNative("methodTest", {"param": "params from flutter"});
    print("invokeNativeMethod "+json.encode(result));
  }
}
